package com.lin.csdn.dao.impl;

import com.lin.csdn.dao.BaseDao;
import com.lin.csdn.dao.intf.ICommentDao;
import com.lin.csdn.model.po.Comment;
import com.lin.csdn.model.vo.NotifyVO;

import java.util.ArrayList;
import java.util.List;

public class CommentDao extends BaseDao implements ICommentDao {

    @Override
    public Object blogIdSearch(int blogId,int beginIndex,int pageSize){
        List<Object> list=new ArrayList<>();
        Object data;
        list.add(blogId);
        Class<Comment> cls=Comment.class;
        String sql="SELECT * FROM comment where blogid=? LIMIT "+beginIndex+","+pageSize;
        data=find(sql,list,cls);
        return data;
    }

    @Override
    public Object userNameSearch(String userName,int beginIndex,int pageSize){
        List<Object> list=new ArrayList<>();
        Object data;
        list.add(userName);
        Class<Comment> cls=Comment.class;
        String sql="SELECT * FROM comment where username=? LIMIT "+beginIndex+","+pageSize;
        data=find(sql,list,cls);
        return data;
    }

    @Override
    public void commentIdDelete(int commentId){
        List<Object> list=new ArrayList<>();
        list.add(commentId);
        update("DELETE FROM comment where id=?",list);
    }

    @Override
    public int addComment(String context,int blogId,String date,String userName){
        List<Object> list=new ArrayList<>();
        list.add(context);
        list.add(blogId);
        list.add(date);
        list.add(userName);
        return update("INSERT INTO comment (context,blogid,date,username) VALUES (?,?,?,?)",list);
    }

    public void addNotify(String userName,String blogTitle,String targetName,String date){
        List<Object> list=new ArrayList<>();
        String context="你在标题为"+blogTitle+"的博客中被"+userName+"提到.";
        list.add(context);
        list.add(targetName);
        list.add(date);
        update("INSERT INTO notify (context,targetname,date) VALUES (?,?,?)",list);
    }

    public Object searchNotify(String userName){
        List<Object> list=new ArrayList<>();
        list.add(userName);
        Class<NotifyVO> cls=NotifyVO.class;
        return find("SELECT * FROM notify WHERE targetname=?",list,cls);
    }
}
